# -*- coding:utf-8 -*-
from site_packages.utils.job import DataOp
from site_packages.ml_libs.nlp.sampling import TfidfReplacement
from datasets import load_cleaned_data, load_clothing_data
from site_packages.ml_libs.nlp.baidu import LinkedTranslater
from evaluation import Generator
import configs.settings as conf
import json


def augment_by_tfidf_replacement():
    """tfidf+embedding替换"""
    # 载入word2vec模型
    word2vec = DataOp.load_model('word2vec')

    # 载入清洗后的数据
    df = load_cleaned_data()

    # 进行tfidf替换处理
    references = df['reference'].values.tolist()
    replace_num = 2
    replacer = TfidfReplacement(replace_num=replace_num, thresh=20, embedding_model=word2vec)
    replaced_corpus = replacer(references)

    # 保存
    df_replaced = df.copy()
    df_replaced['reference'] = replaced_corpus
    df_replaced['reference'] = df_replaced['reference'].apply(lambda x: x.split())
    DataOp.save(df_replaced, 'data_replaced', is_model=False)


def translate():
    """
    回译
    测试成功，结果如下：
    src：三合一混纺纱线制成，柔软亲肤，贴身穿也没有扎感。半开领的立领设计，在较凉的天气，保护脖颈，穿脱也更为方便。侧袖的拼接撞色设计，凸现个性，宝宝穿上更帅气。
    text1: It is made of three in one blended yarn, which is soft and close to the skin without binding feeling. The stand collar design with half open collar can protect neck and make it more convenient to put on and off in cooler weather. The color contrast design of the side sleeve highlights the personality and makes the baby more handsome.
    text2: それは、柔らかくて、結合感なしで皮膚に近い1つの混合糸の3つでできています。半分の開いた襟によるスタンドカラー設計は首を保護することができて、それをより涼しい天気でオンとオフに置くことをより便利にします。サイドスリーブのカラーコントラストのデザインは、性格を強調し、赤ちゃんをよりハンサムにします。
    text3: 它由三条柔软、无结合感、接近皮肤的混合线组成。半开领立领设计可以保护脖子，使其在更凉爽的天气里放在开和关更方便。侧袖的色彩对比设计，突出性格，让宝宝更帅。
    """
    translater = LinkedTranslater(['zh', 'en', 'jp', 'zh'])
    df = load_clothing_data()
    references = df['reference'].values.tolist()
    results = []
    for ref in references:
        print(ref)
        result = translater(ref)
        results.append(result)
        break
    df['reference'] = results
    DataOp.save(df, 'data_translated', is_model=False)


def boostrap_by_ptn():
    """利用ptn生成新样本"""
    # 读取测试数据
    with open(conf.DATA_PATH + '/服饰数据.json', 'r', encoding='utf-8') as f:
        json_dicts = json.load(f)

    # 进行句子生成
    rec_ids = [1, 2, 3, 4, 5]
    raw_inputs = {rec_id: json_dicts[str(rec_id)] for rec_id in rec_ids}
    generator = Generator(beam_search=False, pointer=conf.MODEL_CONF['pointer'], is_coverage=conf.MODEL_CONF['is_coverage'])
    rec_ids, results, references = generator(raw_inputs)
    print(references)


if __name__ == '__main__':
    augment_by_tfidf_replacement()
    translate()
    boostrap_by_ptn()